package com.mano233.server.controller;

import com.mano233.server.config.security.UserCustomDetails;
import com.mano233.server.po.FormUserEditor;
import com.mano233.server.po.ResponseMessage;
import com.mano233.server.po.ResponseStatus;
import com.mano233.server.service.serviceImpl.EventsServiceImpl;
import com.mano233.server.service.serviceImpl.FormServiceImpl;
import com.mano233.server.vo.FormVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;

/**
 * @author mano233
 */
@CrossOrigin(maxAge = 3600)
@Api("表单相关接口")
@RestController
@Validated
public class FormController {
    private final EventsServiceImpl eventsServiceImpl;
    private final FormServiceImpl formService;

    @Autowired
    public FormController(EventsServiceImpl eventsServiceImpl, FormServiceImpl formService) {
        this.eventsServiceImpl = eventsServiceImpl;
        this.formService = formService;
    }

    @GetMapping("/form/{formId}")
    @ApiOperation(value = "根据id查询表单")
    public ResponseMessage getForm(@PathVariable int formId) {
        FormUserEditor userEditor = formService.getFormById(formId);
        if (userEditor == null) {
            return new ResponseMessage(ResponseStatus.ERROR, "表单已删除，或者不存在");
        }
        return new ResponseMessage(ResponseStatus.SUCCESS, userEditor);
    }

    @DeleteMapping("/form/{formId}")
    @ApiOperation(value = "删除表单")
    public ResponseMessage deleteForm(@PathVariable @NotNull int formId, Authentication authentication) {
        UserCustomDetails details = (UserCustomDetails) authentication.getPrincipal();
        FormUserEditor userEditor = formService.getFormById(formId);
        if (userEditor == null || !(details.getUserId().equals(userEditor.getFormCreatorId())) || "admin".equals(details.getUserType())) {
            return new ResponseMessage(ResponseStatus.ERROR, "无权删除");
        }
        if (userEditor.getFormStatus() == 3) {
            return new ResponseMessage(ResponseStatus.ERROR, "无法删除已经报名的表单");
        }
        formService.deleteFormById(details.getUserId(), formId);
        return new ResponseMessage(ResponseStatus.INFO, "删除表单成功");
    }

    @PutMapping("/form")
    @ApiOperation(value = "更新表单")
    public ResponseMessage updateForm(@RequestBody FormVo updateForm, Authentication authentication) {
        UserCustomDetails details = (UserCustomDetails) authentication.getPrincipal();
        int updateFormId = updateForm.getUpdateEditor().getFormId();
        FormUserEditor userEditor = formService.getFormById(updateFormId);
        if (userEditor == null || !(details.getUserId().equals(userEditor.getFormCreatorId()))) {
            return new ResponseMessage(ResponseStatus.ERROR, "无权更新");
        }
        int state = userEditor.getFormStatus();
        if (state == 2 || state == 3) {
            return new ResponseMessage(ResponseStatus.ERROR, "当前表单状态禁止修改");
        }
        FormUserEditor updateReturn = formService.updateForm(updateForm.getUpdateEditor(),
                updateForm.getInsertUserList(), updateForm.getDeleteUserList());
        return new ResponseMessage(ResponseStatus.INFO, updateReturn, "更新成功");
    }

    @PutMapping("/form/access")
    @ApiOperation(value = "更新表单状态")
    public ResponseMessage accessForm(int formId, Authentication authentication) {
        UserCustomDetails details = (UserCustomDetails) authentication.getPrincipal();
        formService.accessForm(formId, details.getUserId());
        return new ResponseMessage(ResponseStatus.INFO, "成功通过");
    }

    @PostMapping("/form/file")
    @ApiOperation(value = "表单文件上传")
    public ResponseMessage uploadFile(@NotNull int formId, @RequestParam("upload") MultipartFile file, Authentication authentication) {
        UserCustomDetails details = (UserCustomDetails) authentication.getPrincipal();
        if (file.isEmpty()) {
            return new ResponseMessage(ResponseStatus.WARN, "禁止上传空文件");
        }
        try {
            ResponseMessage responseMessage = new ResponseMessage(ResponseStatus.INFO);
            responseMessage.setUrl(formService.uploadFile(file, details.getUserId(), formId));
            responseMessage.setMsg("上传成功");
            return responseMessage;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseMessage(ResponseStatus.ERROR, "上传失败，请重试");
        }
    }

    @DeleteMapping("/form/file")
    @ApiOperation(value = "表单文件删除")
    public ResponseMessage deleteFile(int formId, int fileId, String fileURL) throws Exception {
        formService.deleteFile(formId, fileId, fileURL);
        return new ResponseMessage(ResponseStatus.SUCCESS, "删除成功");
    }

    @GetMapping("/form/file")
    @ApiOperation(value = "获取表单文件列表")
    public ResponseMessage getFileList(@NotNull int formId) {
        return new ResponseMessage(ResponseStatus.SUCCESS, formService.getFileList(formId));
    }
}
